package com.xindaoapp.happypet;

import android.content.Intent;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.HasNew;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.MoccaPreferences;

public class BaiduMapManager {
    private LocationClient mLocationClient;

    public void initBaiduMaps() {
        mLocationClient = new LocationClient(HappyPetApplication.get()); // 声明LocationClient类
        mLocationClient.registerLocationListener(myListener); // 注册监听函数
        mLocationClient.start();

        if (CommonUtil.isNetworkAvailable(HappyPetApplication.get()) != 0) {
            InitLocation();
        }
    }

    protected void onStop() {
        mLocationClient.stop();
        mLocationClient.unRegisterLocationListener(myListener);
    }

    public BDLocationListener myListener = new MyLocationListener();

    private class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            String address = location.getAddrStr();
            String friend =
                    MoccaPreferences.HaoYouTid.get() == null ? "" : MoccaPreferences.HaoYouTid
                            .get();
            String meitu =
                    MoccaPreferences.MeiTuTid.get() == null ? "" : MoccaPreferences.MeiTuTid.get();
            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .userLocalCheck(friend, meitu, lat, lng, address, "",
                            android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL,
                            "android", android.os.Build.VERSION.RELEASE,
                            CommonUtil.getVersionName(HappyPetApplication.get().getBaseContext()),
                            new IRequest<HasNew>() {

                                @Override
                                public void request(HasNew t) {
                                    if (t != null) {
                                        Intent intent = new Intent(Constants.ACTION_HASNEW);
                                        intent.putExtra("zuixin", t.isnew);
                                        intent.putExtra("friend", t.follow_new);
                                        intent.putExtra("meitu", t.pic_new);
                                        HappyPetApplication.get().getBaseContext()
                                                .sendBroadcast(intent);
                                    }
                                    onStop();
                                }
                            });
        }

        @Override
        public void onReceivePoi(BDLocation arg0) {
            // TODO Auto-generated method stub

        }

    }

    private void InitLocation() {
        LocationClientOption option = new LocationClientOption();
        // option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
        option.setOpenGps(false);// 打开gps
        option.setCoorType("gcj02");// 返回的定位结果是百度经纬度，默认值gcj02
        option.setScanSpan(20000);// 设置发起定位请求的间隔时间为5000ms
        // option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);

    }
}
